package com.xjtu.platform.user.consultor.addconsult.imply;

import com.xjtu.platform.common.Utils;
import com.xjtu.platform.user.consultor.addconsult.AddConsultDto;
import com.xjtu.platform.user.consultor.addconsult.AddConsultService;
import com.xjtu.platform.user.consultor.addconsult.AdditionDto;
import com.xjtu.platform.user.consultor.addconsult.dao.AddConsultDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service
@Transactional
public class AddConsultImply implements AddConsultService {

    @Resource
    AddConsultDao addConsultDao;

    @Override
    public Map<String, Object> getInfo(AddConsultDto dto) {
        return Utils.getPage(dto,()->addConsultDao.getInfo(dto.getUser_id()));
    }

    @Override
    public boolean addConsult(AdditionDto dto) {


        int check = checkAdd(dto);
        if(check == 100){
            int countnum;
            if (addConsultDao.getCount(dto)==null){countnum = 0;}else {countnum = addConsultDao.getCount(dto).intValue();}
            dto.setCount(countnum + 1);
            int count = addConsultDao.Count();
            dto.setApply_id(count+"");
            addConsultDao.addConsult(dto);
            return true;
        }else return false;

    }

    @Override
    public int checkAdd(AdditionDto dto) {

        if(addConsultDao.checkAdd(dto) == null){
            return 100;
        }
        return addConsultDao.checkAdd(dto).intValue();
    }



    public int getResult(AdditionDto dto){

        if (addConsultDao.getCount(dto)==null){dto.setCount(0);}else{dto.setCount(addConsultDao.getCount(dto));}
        if (addConsultDao.getCount(dto)==null){return 500;}else {return addConsultDao.getResult(dto).intValue();}



    }
}
